package stark.app.function.selectmedia;

import android.view.ViewGroup;

import com.stark.picselect.PhotoSelectorBuilder;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.interfaces.OnSelectResultListener;

import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import stark.app.R;
import stark.app.databinding.ActivitySelectMediaDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.utils.LogUtil;

/**
 * Author：daizhongbin on 2021/3/15
 * Email：1027154338@qq.com
 * Desc: 图片视频素材选择示例
 */
public class SelectMediaDemoActivity extends BaseNoModelActivity<ActivitySelectMediaDemoBinding> {

    private static final int SPAN_COUNT = 4;
    private SelectMediaAdapter mSelectMediaAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_select_media_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnAll.setOnClickListener(view -> {
            selectMedia(PhotoConfig.Mode.ALL, false);
        });
        mDataBinding.btnVideo.setOnClickListener(view -> {
            selectMedia(PhotoConfig.Mode.VIDEO, false);
        });
        mDataBinding.btnPic.setOnClickListener(view -> {
            selectMedia(PhotoConfig.Mode.PHOTO, false);
        });
        mDataBinding.btnCopyPrivate.setOnClickListener(view -> {
            selectMedia(PhotoConfig.Mode.ALL, true);
        });
        GridLayoutManager layoutManager = new GridLayoutManager(this, SPAN_COUNT) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                return new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        };
        mDataBinding.rvPic.setLayoutManager(layoutManager);
        //创建adapter
        mSelectMediaAdapter = new SelectMediaAdapter();
        mDataBinding.rvPic.setAdapter(mSelectMediaAdapter);
    }

    @Override
    protected void initData() {
    }

    private void selectMedia(PhotoConfig.Mode mode, boolean copyPrivate) {
        PhotoSelectorBuilder.builder(this)
                .mode(mode)
                .isCopyToPrivate(copyPrivate)
                .listener(new OnSelectResultListener<List<SelectMediaEntity>>() {
                    @Override
                    public void onResult(List<SelectMediaEntity> data) {
                        LogUtil.e(data.get(0).getTargetPath().toString(), data.get(0).getWidth(), data.get(0).getHeight());
                        if (mSelectMediaAdapter != null) {
                            mSelectMediaAdapter.setNewInstance(data);
                        }
                    }
                });
    }

}
